package com.knowledgegraph.repository;

import com.knowledgegraph.entity.NodeResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NodeResourceRepository extends JpaRepository<NodeResource, Long> {
    
    List<NodeResource> findByNodeId(Long nodeId);
    
    List<NodeResource> findByResourceId(Long resourceId);
    
    List<NodeResource> findByRelationType(String relationType);
    
    @Query("SELECT nr FROM NodeResource nr WHERE nr.node.id = :nodeId AND nr.resource.id = :resourceId")
    List<NodeResource> findByNodeAndResource(@Param("nodeId") Long nodeId, @Param("resourceId") Long resourceId);
    
    @Query("SELECT DISTINCT nr.relationType FROM NodeResource nr")
    List<String> findAllRelationTypes();
    
    @Query("SELECT COUNT(nr) FROM NodeResource nr WHERE nr.relationType = :relationType")
    Long countByRelationType(@Param("relationType") String relationType);
    
    @Query("SELECT nr FROM NodeResource nr WHERE nr.weight >= :minWeight")
    List<NodeResource> findByWeightGreaterThanEqual(@Param("minWeight") Double minWeight);
    
    @Query("SELECT COUNT(nr) FROM NodeResource nr WHERE nr.node.id = :nodeId")
    Long countResourcesByNodeId(@Param("nodeId") Long nodeId);
    
    @Query("SELECT COUNT(nr) FROM NodeResource nr WHERE nr.resource.id = :resourceId")
    Long countNodesByResourceId(@Param("resourceId") Long resourceId);
}